package com.m1book.domain.util;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import com.m1book.domain.user.Miser;

public class MailSender {
	private static final Logger logger = Logger.getLogger(MailSender.class);
	
	public static void sendInvite(String joinPath, String inviteSN, Miser inviter, String qqNum, String inviteeName) {
		JavaMailSenderImpl  mailSender = new JavaMailSenderImpl();
		mailSender.setHost("smtp.gmail.com");
		mailSender.setPort(465);
		mailSender.setProtocol("smtps");
		mailSender.setDefaultEncoding("UTF-8");
		mailSender.setUsername("sanesail@gmail.com");
		mailSender.setPassword("kingSAIL_1979");
		
		Properties prop = new Properties();
		prop.put("mail.smtp.auth", true);
		prop.put("mail.smtp.starttls.enable", true);
		mailSender.setJavaMailProperties(prop);
		
		SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(inviteeName + "<" + qqNum + "@qq.com>");
        mailMessage.setFrom("理智猫<sanesail@gmail.com>");
        mailMessage.setSubject("邀请加入<咪书网>");
        mailMessage.setText("请加入咪书，和我一起分享读书生活。点击此链接进入：" + joinPath + "?sn=" + inviteSN);
        logger.info("发送邀请邮件成功：inviter[" + inviter.getId() + "]; inviteSN[" + inviteSN + "]; qq:[" + qqNum + "]");
        
        mailSender.send(mailMessage);
	}

}
